package com.ruoyi.yljf.controller.AppController;

import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.security.annotation.Anonymous;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.yljf.domain.News;
import com.ruoyi.yljf.domain.dto.NewsDTO;
import com.ruoyi.yljf.domain.vo.NewsVO;
import com.ruoyi.yljf.service.INewsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;
import java.util.List;

/**
 * 系统消息Controller
 * 
 * @author TTTc
 * @date 2025-02-27
 */
@RestController
@RequestMapping("/api/news")
public class ApiNewsController extends BaseController {
    @Autowired
    private INewsService newsService;

    /**
     * 查询系统消息列表 Integer pageNum, Integer pageSize
     */
    @GetMapping("/list")
    public TableDataInfo list() {
        startPage();
        News news = new News();
        news.setUserId(getUserId());
        List<News> list = newsService.selectNewsList(news);
        return getDataTable(list);
    }

    /**
     * 获取系统消息详细信息
     */
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(newsService.getById(id));
    }

    /**
     * 新增系统消息
     */
    @Log(title = "系统消息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody News news) {
        return toAjax(newsService.save(news));
    }


    /**
     * 删除系统消息
     */
    @Log(title = "系统消息", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(newsService.removeByIds(Arrays.asList(ids)));
    }

    /**
     * 一键已读
     */
    @Log(title = "一键已读", businessType = BusinessType.UPDATE)
    @PutMapping("/allRead")
    public AjaxResult allRead() {
        int i = newsService.allRead(getUserId());
        return success();
    }
}
